package com.kd.crawler.account.weibo;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.kd.crawler.entity.Document;
import com.kd.crawler.entity.WeiboDoc;
import com.kd.crawler.jms.ActiveMQService;
import com.kd.crawler.utils.HttpRequestUtils;
import com.kd.crawler.utils.PropertiesUtils;

public class WeiboAccountDataTransport {

	private static Logger logger = Logger.getLogger(WeiboAccountDataTransport.class);
	
	public static void main(String[] args) {
		WeiboAccountDataTransport wdt = new WeiboAccountDataTransport();
		wdt.doTransport();
	}
	
	public void doTransport() {
		logger.debug("start to transport weiboAccount data...");
		Properties pro = PropertiesUtils.getProperties("weibo.properties");
		String searchUrl = pro.getProperty("url.search");
		String updateUrl = pro.getProperty("url.update");
		int restTime = Integer.parseInt(pro.getProperty("rest.time", "180"));
		int limit = Integer.parseInt(pro.getProperty("limit", "20"));
		Properties AMQPro = PropertiesUtils.getProperties("activeMQ.properties");
		String docMessageTopic = AMQPro.getProperty("QUEUE_CRAWL_DOC_MESSAGE");
		if(null==searchUrl || "".equals(searchUrl.trim())) {
			throw new RuntimeException("This url is null for  weibo account informant to search!");
		}
		if(null==updateUrl || "".equals(updateUrl.trim())) {
			throw new RuntimeException("This url is null for  weibo account informant to update!");
		}
		logger.info("transporting weiboAccount data... ");
		Map<String, String> searchParams = new HashMap<String, String>();
		searchParams.put("limit", limit+"");
		while(true) {
			JSONArray jsonArray = null;
			try { 
				jsonArray = HttpRequestUtils.sendPostReturnJSONArray(searchUrl, searchParams);
			} catch(Exception e) {
				logger.error("failed to select some weiboAccount data from database!", e);
			}
			if(null==jsonArray || jsonArray.isEmpty()) {
				try {
					System.out.println("There are no any weibo data, now, Please wait for " + restTime + " seconds...");
					Thread.sleep(restTime*1000);
					continue ;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			for(int i=0; i<jsonArray.size(); i++) {
				WeiboDoc weiboDoc = jsonArray.getObject(i, WeiboDoc.class);
				Map<String, String> updateParams = new HashMap<String, String>();
				updateParams.put("url", weiboDoc.getUrl());
				String content = weiboDoc.getContent();
				if(null==content || "".equals(content.trim())) {
					HttpRequestUtils.sendPost(updateUrl, updateParams);
					continue ;
				}
				// http://weibo.com/3511885300/dla2y6bbl?from=page_1005053511885300_profile&wvr=6&mod=weibotime
//				System.out.println(HttpRequestUtil.sendPost(updateUrl, updateParams));
				// reverse weiboDoc into Document
				Document doc = new Document();
				doc.setUrl(weiboDoc.getUrl());
				doc.setTitle(weiboDoc.getTitle());
				doc.setAuthor(weiboDoc.getAuthor());
				doc.setDate(weiboDoc.getDate());
				doc.setContent(weiboDoc.getContent());
				doc.setClickNum(weiboDoc.getClickNum());
				doc.setCommentNum(weiboDoc.getCommentNum());
				doc.setJobId(weiboDoc.getSource());
				doc.setSource(weiboDoc.getSource());
				doc.setType(weiboDoc.getType());
				doc.setLevel(weiboDoc.getLevel());
				int length = 12 > content.length() ? content.length() : 12;
				String title = content.substring(0, length);
				doc.setTitle(title);
				doc.setLevel(2);
				logger.info("start to enqueue to JMS for the document: " + doc.logFor());
				// send a document to JMS
				if(ActiveMQService.sendTopic(docMessageTopic, doc)) {
					System.out.println(HttpRequestUtils.sendPost(updateUrl, updateParams));
					logger.info("Success to enqueue to JMS for the document: " + doc.logFor());
				} else {
					logger.warn("Failed to enqueue to JMS for the document: " + doc.logFor());
				}
			}
		}
	}
	
}
